﻿#ifndef CATHTTPTOOLS_H
#define CATHTTPTOOLS_H

#include <QThread>
#include <QNetworkReply>
#include <QMutex>
#include <QWaitCondition>

class QFile;

class CatHttpTools : public QThread
{
    Q_OBJECT
public:
    explicit CatHttpTools(QObject *parent = nullptr);
    ~CatHttpTools();

public:
    enum STATE
    {
        INOPERATION,
        DOWNLOAD
    };

    void DownLoad(QUrl url, QString downloadir = "./", bool ssl = false);

private:
    int DownLoad();

    void DeleteDownLoad();

protected:
    void run();

signals:
    void DownLoadFinished(const QString &filePath);
    void DownLoadError();
    void DownLoadProgress(qint64, qint64);

private slots:
    void On_HttpFinished();
    void On_HttpReadyRead();
    void On_HttpError(QNetworkReply::NetworkError);
    void On_UpdateDataReadProgress(qint64, qint64);

private:
    bool m_bSSL;
    STATE m_yState;

    QUrl m_pUrl;
    QString m_sDownLoadPath;
    QFile* DownLoadFile = nullptr;
    QNetworkAccessManager* Manager = nullptr;
    QNetworkReply* Reply = nullptr;
    QMutex m_yMutex;
    QWaitCondition m_yCondition;
};

#endif // CATHTTPTOOLS_H
